package com.jushu.storbox.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jushu.storbox.R;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.entities.FileFolderInfo;
import com.huawei.sharedrive.sdk.android.modelV3.response.LinkInfoV3;
import com.huawei.sharedrive.sdk.android.util.StringUtil;

public class LinkListAdapter extends AbsTemplateAdapter<LinkInfoV3> {
	private List<LinkInfoV3> linkList;
	private Context context;
	private ViewHolder viewHolder;
	private FileFolderInfo nodeInfo;

	public LinkListAdapter(Context context, int resource, FileFolderInfo nodeInfo, List<LinkInfoV3> objects) {
		super(objects);
		this.nodeInfo = nodeInfo;
		this.linkList = objects;
		this.context = context;
	}

	public List<LinkInfoV3> getLinkList() {
		return linkList;
	}

	public void setLinkList(List<LinkInfoV3> linkList) {
		this.linkList = linkList;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (null == convertView) {
			convertView = LayoutInflater.from(context).inflate(R.layout.link_list_item, null);
			viewHolder = new ViewHolder();
			viewHolder.linkUrl = (TextView) convertView.findViewById(R.id.linkUrl);
			viewHolder.linkAuthorize = (TextView) convertView.findViewById(R.id.accessTextId);
			viewHolder.linkCode = (TextView) convertView.findViewById(R.id.codeTextId);
			viewHolder.linkValidDate = (TextView) convertView.findViewById(R.id.dateTextId);
			viewHolder.copyImageView = (ImageView) convertView.findViewById(R.id.linkCopyImg);
			viewHolder.editImageView = (ImageView) convertView.findViewById(R.id.linkEditImg);
			viewHolder.shareImageView = (ImageView) convertView.findViewById(R.id.linkShareImg);
			viewHolder.deleteImageView = (ImageView) convertView.findViewById(R.id.linkDeleteImg);
			convertView.setTag(viewHolder);
		}
		viewHolder = (ViewHolder) convertView.getTag();
		final LinkInfoV3 linkInfo = linkList.get(position);

		SharedPreferences settings = context.getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
		String address = settings.getString(ClientConfig.SERVER_ADDRESS, "");
		if (address.trim().equals("")) {
			address = ClientConfig.SERVICEADD;
		}
		StringBuffer tmp = new StringBuffer(address);
		tmp.append("/").append("p").append("/").append(linkInfo.getId());
		final String urlStr = tmp.toString();
		final String plainAccessCode = linkInfo.getPlainAccessCode();

		Resources res = context.getResources();
		viewHolder.linkUrl.setText(urlStr);
		viewHolder.linkCode.setText(plainAccessCode);
		try{
			viewHolder.linkAuthorize.setText(res.getIdentifier(linkInfo.getRole(), "string", context.getPackageName()));
		}catch(Exception e){
			viewHolder.linkAuthorize.setText("");
		}

		viewHolder.copyImageView.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
				cmb.setText(urlStr);
				Toast.makeText(context, context.getString(R.string.copy_success), Toast.LENGTH_SHORT).show();
			}
		});
		viewHolder.editImageView.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				editLink(nodeInfo, linkInfo);
			}
		});
		viewHolder.shareImageView.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				StringBuffer tmp = new StringBuffer();
				tmp.append(urlStr);
				if (StringUtil.isNotBlank(plainAccessCode)) {
					tmp.append("\r\n").append(context.getString((R.string.is_control))).append(plainAccessCode);
				}

				Intent sendLinkMail = new Intent(Intent.ACTION_SEND);
				sendLinkMail.setType("text/plain");
				sendLinkMail.putExtra(Intent.EXTRA_EMAIL, "");
				sendLinkMail.putExtra(Intent.EXTRA_SUBJECT, context.getString((R.string.share_link_title)));

				sendLinkMail.putExtra(Intent.EXTRA_TEXT, tmp.toString());
				sendLinkMail.putExtra("linkUrl", urlStr);
				sendLinkMail.putExtra("nodeId", nodeInfo.getId());
				sendLinkMail.putExtra("nodeName", nodeInfo.getName());
				sendLinkMail.putExtra("plainAccessCode", plainAccessCode);
				context.startActivity(Intent.createChooser(sendLinkMail, context.getString(R.string.send_link_choose)));
			}
		});
		viewHolder.deleteImageView.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				deleteLink(nodeInfo, linkInfo);
			}
		});

		Long effectiveAt = linkInfo.getEffectiveAt();
		if (effectiveAt != null) {
			StringBuffer tipBuffer = new StringBuffer();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			tipBuffer.append(sdf.format(new Date(effectiveAt)));
			Long expireAt = linkInfo.getExpireAt();
			if (expireAt != null) {
				tipBuffer.append(context.getString(R.string.to));
				tipBuffer.append(sdf.format(new Date(expireAt)));
			} else {
				tipBuffer.append(context.getString(R.string.to) + " " + context.getString(R.string.forever));
			}
			viewHolder.linkValidDate.setText(tipBuffer.toString());
		} else {
			viewHolder.linkValidDate.setText("");
		}

		return convertView;
	}

	public void deleteLink(FileFolderInfo node, LinkInfoV3 linkInfo) {
	}

	public void editLink(FileFolderInfo node, LinkInfoV3 linkInfo) {
	}

	public static class ViewHolder {
		public TextView linkUrl, linkCode, linkAuthorize, linkValidDate;
		public ImageView copyImageView;
		public ImageView editImageView;
		public ImageView shareImageView;
		public ImageView deleteImageView;
	}
}
